package com.sixth.aspect;

import com.sixth.anotation.PreAuthoritory;
import com.sixth.core.util.JWTUtil;
import com.sixth.core.util.WebUtil;
import com.sixth.core.vo.R;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;

/**
* @program: sixth-hospital-parent
*
* @description: 
*
* @author: 李劲锋
*
* @create: 2024-08-13 15:59
**/
@Aspect
@Component
public class AuthProxy {

    //切点
    @Pointcut("@annotation(com.sixth.anotation.PreAuthoritory)")
    private void pointcut(){}
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取使用该注解的方法对象
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        //获取方法上的注解对象
        PreAuthoritory annotation = method.getAnnotation(PreAuthoritory.class);
        //获取注解的值
        String value = annotation.value();
        //判断注解的值 是否在当前用户具有的权限中
        String token = WebUtil.getRequest().getHeader("token");
        List<String> permissions = (List<String>) JWTUtil.getInfo(token).get("permissions");
        if(!permissions.contains(value)){
            return R.error("权限不足");
        }
        return joinPoint.proceed(); //执行方法
    }
}